<?php
namespace app\platform\controller;
use app\BaseController;
use app\common\model\OrderModel;
use app\common\logic\DataSettingLogic;
use app\common\model\AppModel;
use app\common\helper\payment\PaymentPlatformHelper;
//平台的支付请求及回调
class  Payment extends BaseController{
    
    public function sms(){
        $DataSettingLogic = new DataSettingLogic();
        $sms = $DataSettingLogic->getData('sms');
        if(empty($sms['price'])){
            return $this->error('该应用还未开通短信充值服务');
        }
        $skuIndex = $this->request->param('skuIndex/d');
        if(!isset($sms['price'][$skuIndex])){
            return $this->error('该应用还未开通短信充值服务');
        }
        $orderInfo = [
             'shop_id'  => $this->request->shopId,
             'type'     => 2,
             'app_name' => $this->request->app->iden,
             'num'      => $sms['price'][$skuIndex]['num'],
             'price'    => $sms['price'][$skuIndex]['price'],
             'is_paid'  => 0,
         ];
         $orderModel = new OrderModel();
         $orderModel->save($orderInfo);
         $orderId = $orderModel->order_id;
         $PaymentPlatformHelper = new PaymentPlatformHelper();
         $res = $PaymentPlatformHelper->getPayment($orderId);
         if($res === false){
             return $this->error('创建支付订单失败');
         }
         return  $this->success([
             'order_id' => $orderId,
             'code_url' => $res
         ]);
    }
    
    public function app(){
         $sku = $this->request->app->sku;
         if(empty($sku)){
             return $this->error('该应用还未开通在线支付');
         }
         $skuIndex = $this->request->param('skuIndex/d');
         if(!isset($sku[$skuIndex])){
             return $this->error('该应用还未开通在线支付');
         }
         $skuDetail = $sku[$skuIndex];
         $num = $skuDetail['num'];
         switch ($skuDetail['unit']){
             case 2:
                 $num = $num * 30;
                 break;
             case 3:
                 $num = $num * 365;
                 break;
         }
         $orderInfo = [
             'shop_id'  => $this->request->shopId,
             'type'     => 1,
             'app_name' => $this->request->app->iden,
             'num'      => $num,
             'price'    => $skuDetail['price'],
             'is_paid'  => 0,
         ];
         $orderModel = new OrderModel();
         $orderModel->save($orderInfo);
         $orderId = $orderModel->order_id;
         $PaymentPlatformHelper = new PaymentPlatformHelper();
         $res = $PaymentPlatformHelper->getPayment($orderId);
         if($res === false){
             return $this->error('创建支付订单失败');
         }
         return  $this->success([
             'order_id' => $orderId,
             'code_url' => $res
         ]);
    }
    
    public function check(){
        $orderId = $this->request->param('order_id/d');
        if(empty($orderId)){
            return  $this->error('要查询的订单不存在');
        }
        if(null === $order = OrderModel::find($orderId)){
            return  $this->error('要查询的订单不存在');
        }
        if($order->shop_id != $this->request->shopId){
            return  $this->error('要查询的订单不存在');
        }
        return $this->success(['status'=>$order->is_paid]);
    }
    
    public function respond(){
        $PaymentPlatformHelper = new PaymentPlatformHelper();
        $PaymentPlatformHelper->respond();
    }
    
}
